package com.service.Impl;

import com.mapper.CFishingShopMapper;
import com.mapper.SysUserMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CFishingShop;
import com.pojo.vo.CFishingShopVo;
import com.pojo.vo.CommentVo;
import com.service.CFishingShopService;
import com.service.CommentService;
import com.util.LoLaToDistanceUtil;
import com.util.PojoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service("CFishingShopService")
public class CFishingShopServiceImpl implements CFishingShopService {
    @Autowired
    CFishingShopMapper cFishingShopMapper;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    CommentService commentService;

    @Override//city和user_num需要多表查询/id，name,contract_user(店铺法人姓名)，authenticate(是否认证）
    public Message<List<CFishingShopVo>> list(CFishingShopVo cFishingShopVo) {
//        Page<CFishingShop> page = new Page();
//        page.setCurrent(cFishingShopVo.getPage());
//        page.setSize(cFishingShopVo.getRows());

        Double latitude = null;
        Double longtitude = null;
        QueryWrapper<CFishingShop> queryWrapper = new QueryWrapper<>();
        if (cFishingShopVo != null) {
            latitude = cFishingShopVo.getLatitude();
            longtitude = cFishingShopVo.getLongtitude();
            if (!StringUtils.isEmpty(cFishingShopVo.getId())) {
                queryWrapper.eq("id", cFishingShopVo.getId());
            }
            if (!StringUtils.isEmpty(cFishingShopVo.getName())) {
                String name = cFishingShopVo.getName();
                queryWrapper.like("name", cFishingShopVo.getName());//名字模糊查询
            }
            if (!StringUtils.isEmpty(cFishingShopVo.getUserNum())) {
//                QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
//                userQueryWrapper.eq("username",cFishingShopVo.getUserNum());
//                SysUser sysUser = sysUserMapper.selectOne(userQueryWrapper);
                queryWrapper.eq("user_num", cFishingShopVo.getUserNum());//店主
            }
            if (!StringUtils.isEmpty(cFishingShopVo.getPhone())) {
                queryWrapper.eq("phone", cFishingShopVo.getPhone());
            }
            if (!StringUtils.isEmpty(cFishingShopVo.getContractUser())) {
                queryWrapper.eq("contract_user", cFishingShopVo.getContractUser());//店铺法人姓名
            }
            if (!StringUtils.isEmpty(cFishingShopVo.getAuthenticate())) {
                queryWrapper.eq("authenticate", cFishingShopVo.getAuthenticate());
            } else {
                queryWrapper.eq("authenticate", 2);
            }
            if (!StringUtils.isEmpty(cFishingShopVo.getCity())) {
//                QueryWrapper<SysCity> cityQueryWrapper = new QueryWrapper<>();
//                cityQueryWrapper.eq("class_name",cFishingShopVo.getCity());
//                SysCity sysCity = sysCityMapper.selectOne(cityQueryWrapper);
                queryWrapper.eq("city", cFishingShopVo.getCity());
            }
        }
//        IPage<CFishingShop> cFishingShopPage = cFishingShopMapper.selectPage(page, queryWrapper);
        List<CFishingShop> cFishingShops = cFishingShopMapper.selectList(queryWrapper);
        List<CFishingShopVo> result = new ArrayList<>();
        for (int i = 0; i < cFishingShops.size(); i++) {
            CFishingShop cFishingShop1 = cFishingShops.get(i);
            Double latitude1 = cFishingShop1.getLatitude();
            Double longtitude1 = cFishingShop1.getLongtitude();
            CFishingShopVo cFishingShopVo1 = PojoUtil.copyObject(cFishingShop1, CFishingShopVo.class);
            //算出商店与现坐标的距离，并返回
            if (latitude != null && longtitude != null && latitude1 != null && longtitude1 != null) {
                double distance = LoLaToDistanceUtil.getDistance(latitude, latitude1, longtitude, longtitude1);
                cFishingShopVo1.setDistance(distance);
            }
            //查出所有的评论
            List<CommentVo> commentVoList = selectComment(cFishingShop1.getId(), 3L);
            cFishingShopVo1.setCommentVoList(commentVoList);
//            cFishingShopVo1.setPage(cFishingShopVo.getPage());
//            cFishingShopVo1.setRows(cFishingShopVo.getRows());
            result.add(cFishingShopVo1);
        }
        return Message.successMessage(1L, result);
    }

    @Override
    public Message<CFishingShopVo> selectOne(Long id) {
        QueryWrapper<CFishingShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        CFishingShop cFishingShop = cFishingShopMapper.selectOne(queryWrapper);
        CFishingShopVo cFishingShopVo = PojoUtil.copyObject(cFishingShop, CFishingShopVo.class);
        List<CommentVo> commentVoList = selectComment(cFishingShop.getId(), 3L);
        cFishingShopVo.setCommentVoList(commentVoList);
        if (cFishingShop != null) return Message.successMessage(1L, cFishingShopVo);
        else return Message.successMessage(1L, null);
    }

    @Override
    public Message<CFishingShopVo> add(CFishingShopVo cFishingShopVo) {
        int i = cFishingShopMapper.insert(PojoUtil.copyObject(cFishingShopVo, CFishingShop.class));
        if (i == 0) return Message.errorMessage("1001", "添加失败");
        else return Message.successMessage(1L, cFishingShopVo);
    }

    @Override
    public Message<CFishingShopVo> edit(CFishingShopVo cFishingShopVo) {
        int i = cFishingShopMapper.updateById(PojoUtil.copyObject(cFishingShopVo, CFishingShop.class));
        if (i == 0) return Message.errorMessage("1001", "更新失败");
        return Message.successMessage(1L, cFishingShopVo);
    }

    @Override
    public Message<CFishingShopVo> delete(Long id) {
        int i = cFishingShopMapper.deleteById(id);
        if (i == 0) return Message.errorMessage("1001", "删除失败");
        return Message.successMessage(1L, null);
    }

    @Override
    public List<CommentVo> selectComment(Long id, Long type) {
        CommentVo cComment = new CommentVo();
        cComment.setArticleId(id);
        cComment.setCommentType(type);
        Message<List<CommentVo>> list = commentService.list(cComment);
        List<CommentVo> commentVoList = new ArrayList<>();
        for (int i = 0; i < list.getData().size(); i++) {
            commentVoList.add(PojoUtil.copyObject(list.getData().get(i), CommentVo.class));
        }
        return commentVoList;
    }

}
